#!/usr/bin/env python
#coding=utf-8
# Copyrignt (c) qkier.com. All Rights Reserved.
# Author: zengxiaobao@qkier.com
# Created Time: 2017-05-31 19:16:39
# Last Modified: 2017-06-14 17:12:01
# File Name: geturl.py
# Description: 

__version__ = "0.1"
__all__ = []

import os
import sys
import hashlib
import json
import copy
import time

#host = "127.0.0.1:8122"
#host = "test00.sz.doubimeizhi.com"
host="qkstat.app.qkier.com"
salt = "585d035ae9584f0f84ce6aa1500ed2ab"
default_params = {
    "appkey": "62ac59ca9ba2415898438c36dcc34f53",
    "time": int(time.time()),
   # "ak": "3c1997db10d74ffc98b558713a7b3248",
   # "uid": "baa6ffa5bb204f13b834c161a3d56ac7"
}

def get_url(path, params):
    myparams = copy.deepcopy(default_params)
    myparams.update(params)
    keys = myparams.keys()
    keys.sort()
    calc_arr = []
    for key in keys:
        calc_arr.append(key)
        calc_arr.append(str(myparams[key]))
    calc_arr.append(salt)
    m = hashlib.md5()
    m.update("".join(calc_arr))
    sign = m.hexdigest()
    myparams["sign"] = sign
    qs_arr = []
    for k, v in myparams.iteritems():
        qs_arr.append("%s=%s" % (k, v))
    qs = "&".join(qs_arr)
    return "https://%s%s?%s" % (host, path, qs)

if __name__  == "__main__":
    if len(sys.argv) != 3:
        print "%s [path] [params]" % sys.argv[0]
        sys.exit(1)
    path = sys.argv[1]
    params = json.loads(sys.argv[2])
    print get_url(path, params)
